import MapManageBase from './MapManageBase'

/**
 * 请求响应类型
 */
export type responseStatus =
    | 'init'        // 初始
    | 'ready'       // 准备
    | 'pending'     // 等待
    | 'reslove'     // 成功
    | 'reject'      // 失败

/**
* 请求统计格式
*/
export interface IApiCount {
    count: number;                  // 请求次数
    status: responseStatus;         // 当前请求状态
    lastTime: number;               // 最后一次请求时间
    createdTime: number;            // 请求创建时间
}

/**
 * 接口统计类
 */
export default class ApiCount extends MapManageBase<IApiCount> {
    constructor() {
        super({
            count: 1,
            status: 'init',
            lastTime: Date.now(),
            createdTime: Date.now()
        })
    }
    /**
     * 接口统计准备
     * @param url 
     */
    readyApiCount(url: string): void {
        let initApi: IApiCount = Object.assign({}, this.initMapType)
        initApi.status = 'ready'
        if (this.hasData(url)) {
            let currentApi = this.getData(url)
            currentApi.count += 1
            currentApi.lastTime = Date.now()
            this.setData(url, currentApi)
        } else {
            this.setData(url, initApi)
        }
    }
    /**
     * 设置接口状态
     * @param url 接口链接
     * @param status 状态值
     */
    setApiState(url: string, status: responseStatus): void {
        let currentApi = this.getData(url)
        currentApi.status = status
        this.setData(url, currentApi)
    }
    /**
     * 获取接口状态
     * @param url 接口链接
     * @returns 
     */
    getApiState(url: string): responseStatus {
        return this.getData(url).status
    }
}